package com.zxx.mvpPractice.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * Created with Android Studio.
 * <p/>
 * Author:xiaxf
 * <p/>
 * Date:2015/7/20.
 */
public class SharePrefMagager {

    private static String TAG = "SharePrefMagager";
    private static SharePrefMagager mSharePrefMagager;
    private Context mContext;


    public static SharePrefMagager getInstance(Context context) {
        if (mSharePrefMagager == null) {
            mSharePrefMagager = new SharePrefMagager(context);
        }
        return mSharePrefMagager;
    }

    private SharePrefMagager(Context context) {
        mContext = context;
    }


    private SharedPreferences getSharedPreferences(String name) {
        if (mContext == null) {
            Log.e(TAG, "getSharedPreferences: mContext null");
            return null;
        }
        return mContext.getSharedPreferences(name, Context.MODE_PRIVATE);
    }

    public void putString(String name, String key, String value) {
        SharedPreferences.Editor editor = getSharedPreferences(name).edit();
        editor.putString(key, value);
        editor.apply();
    }

    public void putInt(String name, String key, int value) {
        SharedPreferences.Editor editor = getSharedPreferences(name).edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public void putLong(String name, String key, long value) {
        SharedPreferences.Editor editor = getSharedPreferences(name).edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public void putFloat(String name, String key, float value) {
        SharedPreferences.Editor editor = getSharedPreferences(name).edit();
        editor.putFloat(key, value);
        editor.apply();
    }

    public void putBoolean(String name, String key, boolean value) {
        SharedPreferences.Editor editor = getSharedPreferences(name).edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public String getString(String name, String key, String defaultValue) {
        SharedPreferences settings = getSharedPreferences(name);
        return settings.getString(key, defaultValue);
    }

    public int getInt(String name, String key, int defaultValue) {
        SharedPreferences settings = getSharedPreferences(name);
        return settings.getInt(key, defaultValue);
    }

    public long getLong(String name, String key, long defaultValue) {
        SharedPreferences settings = getSharedPreferences(name);
        return settings.getLong(key, defaultValue);
    }

    public float getFloat(String name, String key, float defaultValue) {
        SharedPreferences settings = getSharedPreferences(name);
        return settings.getFloat(key, defaultValue);
    }

    public boolean getBoolean(String name, String key, boolean defaultValue) {
        SharedPreferences settings = getSharedPreferences(name);
        return settings.getBoolean(key, defaultValue);
    }

    public void remove(String name, String key) {
        SharedPreferences.Editor editor = getSharedPreferences(name).edit();
        editor.remove(key).apply();
    }
}
